from selenium.webdriver import ActionChains
from hyrobot.common import STEP, CHECK_POINT, INFO
from create_table_super_num import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from pymysql_connect import *

class c73250c:
    # name = '超级管理员进行查询数据库核对'  # 测试用例名字

    cases = [
        ('c73250c', 'sunxidong', '123456'),

    ]

    # 测试用例步骤
    def teststeps(self, para_index):
        create_table_for_superior_num()
        # 取出参数
        # STEP(1,"登录")
        # username, password = self.cases[para_index][1:3]
        # # 下面写登录流程
        # driver = webdriver.Firefox()
        # driver.implicitly_wait(10)
        #
        # driver.get('http://172.17.66.19:8080/')
        #
        # driver.find_element(By.NAME, "username").send_keys(username)
        # driver.find_element(By.NAME, "password").send_keys(password)
        # driver.find_element(By.CSS_SELECTOR, ".el-button").click()

        STEP(2,"核对超级管理员产品列表总数与产品管理员实际创建的可见产品数量是否一致")
        db = connect()
        # 使用cursor()方法获取操作游标
        INFO('数据库已连接')
        cursor = db.cursor()
        sql = "select count(case when is_display=1 then 1 end) as calculate_active_num ,\
               count(case when is_display=0 then 1 end) as calculate_disable_num from test_kylin_web.product_num ;"

        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                active_num = row[0]
                disable_num = row[1]
        except:
            INFO("1Error: unable to fetch data")
        cursor_product_list = db.cursor()
        product_list_sql = "SELECT count(id) as display_num FROM test_kylin_web.product_list;"
        try:
            # 执行SQL语句
            cursor_product_list.execute(product_list_sql)
            # 获取所有记录列表
            product_list_result = cursor_product_list.fetchall()
            for row in product_list_result:
                product_list_active_num = row[0]
        except:
            INFO("2Error: unable to fetch data")
        INFO("未显示数量")
        INFO(disable_num)
        try:
            CHECK_POINT("核对产品列表总数与产品管理员实际创建的可见产品数量是否一致",active_num == product_list_active_num)
        except:
            INFO('否一致')

        STEP(3,"从产品管理员账号的产品列表中挑选10条与超级管理员产品列表核对详细的产品信息")
        cursor_product_list_detail = db.cursor()
        product_list_detail_sql = "SELECT product_name,version,creater,create_time,update_time,product_function,introduction FROM test_kylin_web.product_list " \
                                  "WHERE creater = '张三' OR creater = '张31';"

        try:
            # 执行SQL语句
            cursor_product_list_detail.execute(product_list_detail_sql)
            # 获取所有记录列表
            product_list_detail_result = cursor_product_list_detail.fetchall()
            require_list = list(list(items) for items in list(product_list_detail_result))
        except:
            INFO("Error: unable to fetch data")

        # from productor
        cursor_product_list_detail_productor = db.cursor()
        product_list_detail_productor_sql = "SELECT product_name,version,creater,create_time,update_time,product_function,introduction FROM test_kylin_web.product_list_for_proer " \
                                            "WHERE creater = '张三' OR creater = '张31';"

        try:
            # 执行SQL语句
            cursor_product_list_detail_productor.execute(product_list_detail_productor_sql)
            # 获取所有记录列表
            product_list_detail_productor_result = cursor_product_list_detail_productor.fetchall()
            require_list_productor = list(list(items) for items in list(product_list_detail_productor_result))
            try:
                CHECK_POINT("比较得到数据列表是否一致",sorted(require_list_productor) == sorted(require_list))
            except:
                INFO('否一致')
        except:
            INFO("Error: unable to fetch data")

        db.close()
        # driver.quit()

        STEP(4, "核对超级管理员产品列表总数与数据库实际创建未删除的可见产品数量是否一致")
        kylin_db = kylin_mysql_connect()
        # 使用cursor()方法获取操作游标
        INFO('数据库已连接')
        kylin_cursor = kylin_db.cursor()
        kylin_sql = "select count(case when is_display=1 then 1 end) as calculate_active_num ,\
                       count(case when is_display=0 then 1 end) as calculate_disable_num from customer_service_platform.product_info where is_deleted = 0 ;"

        try:
            # 执行SQL语句
            kylin_cursor.execute(kylin_sql)
            # 获取所有记录列表
            kylin_results = kylin_cursor.fetchall()
            for row in kylin_results:
                kylin_active_num = row[0]
                kylin_disable_num = row[1]
        except:
            INFO("1Error: unable to fetch data")

        INFO("未显示数量")
        INFO(disable_num)
        INFO(kylin_active_num)
        try:
            CHECK_POINT("核对产品列表总数与kylin_database实际创建的可见产品数量是否一致", active_num == kylin_active_num)
        except:
            INFO('数目不一致')
